package com.bjpowernode.crm.workbench.service.impl;

import com.bjpowernode.crm.commons.utils.DateUtils;
import com.bjpowernode.crm.commons.utils.UUIDUtils;
import com.bjpowernode.crm.settings.domain.User;
import com.bjpowernode.crm.workbench.domain.*;
import com.bjpowernode.crm.workbench.mapper.*;
import com.bjpowernode.crm.workbench.service.TranService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 郭俊才
 * 2021/8/30
 */
@Service
public class TranServiceImpl implements TranService {
    @Autowired
    private TranMapper tranMapper;
    @Autowired
    private CustomerMapper customerMapper;
    @Autowired
    private ContactsMapper contactsMapper;
    @Autowired
    private TranHistoryMapper tranHistoryMapper;
    @Autowired
    private ContactsActivityRelationMapper contactsActivityRelationMapper;

    @Override
    public int saveSelective(Map<String, Object> map) {
        Tran tran = (Tran) map.get("tran");
        User user = (User) map.get("user");
        String contactsName = (String) map.get("contactsName");
        String customerName = (String) map.get("customerName");
        String customerId = tran.getCustomerId();
        String contactsId = tran.getContactsId();
        //判断客户id是否为空(空则新建一个客户)
        Customer customer = null;
        if (customerId == null || customerId == "") {
            customer = new Customer();
            customer.setId(UUIDUtils.getUUID());
            customer.setOwner(tran.getOwner());
            customer.setName(customerName);
            customer.setCreateBy(user.getId());
            customer.setCreateTime(DateUtils.formatDateTime(new Date()));
            customer.setDescription(tran.getDescription());
            customer.setContactSummary(tran.getContactSummary());
            customer.setNextContactTime(tran.getNextContactTime());
            customerMapper.insertSelective(customer);
        }
        Contacts contacts = null;
        //判断联系人id是否为空(空则新建一个联系人)
        if (contactsId == null || contactsId == "") {
            contacts = new Contacts();
            contacts.setId(UUIDUtils.getUUID());
            contacts.setOwner(tran.getOwner());
            contacts.setSource(tran.getSource());
            if (customerId == null || customerId == "") {
                contacts.setCustomerId(customer.getId());
            } else {
                contacts.setCustomerId(tran.getCustomerId());
            }
            contacts.setFullName(contactsName);
            contacts.setCreateBy(user.getId());
            contacts.setCreateTime(DateUtils.formatDateTime(new Date()));
            contacts.setDescription(tran.getDescription());
            contacts.setContactSummary(tran.getContactSummary());
            contacts.setNextContactTime(tran.getNextContactTime());
            contactsMapper.insertSelective(contacts);
        }
        //增加联系人和市场活动表记录
        ContactsActivityRelation contactsActivityRelation = new ContactsActivityRelation();
        contactsActivityRelation.setId(UUIDUtils.getUUID());
        contactsActivityRelation.setActivityId(tran.getActivityId());
        if (contactsId == null || contactsId == "") {
            contactsActivityRelation.setContactsId(contacts.getId());
        }else {
            contactsActivityRelation.setContactsId(tran.getContactsId());
        }
        contactsActivityRelationMapper.insertSelective(contactsActivityRelation);
        //设置主键
        tran.setId(UUIDUtils.getUUID());
        //设置创建时间
        //设置创建人
        tran.setCreateBy(user.getId());
        tran.setCreateTime(DateUtils.formatDateTime(new Date()));
        if (customerId == null || customerId == "") {
            tran.setCustomerId(customer.getId());
        }
        if (contactsId == null || contactsId == "") {
            tran.setContactsId(contacts.getId());
        }
        //创建一个交易历史
        TranHistory tranHistory = new TranHistory();
        tranHistory.setId(UUIDUtils.getUUID());
        tranHistory.setTranId(tran.getId());
        tranHistory.setStage(tran.getStage());
        tranHistory.setMoney(tran.getMoney());
        tranHistory.setExpectedDate(tran.getExpectedDate());
        tranHistory.setCreateBy(user.getId());
        tranHistory.setCreateTime(DateUtils.formatDateTime(new Date()));
//        tranHistory.setOrderNo(tran.getOrderNo());
        tranHistoryMapper.insertSelective(tranHistory);
        return tranMapper.insertSelective(tran);
    }

    @Override
    public int removeByPrimaryKey(String id) {
        return tranMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int editByPrimaryKeySelective(Tran record) {
        return tranMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public Tran queryByPrimaryKey(String id) {
        return tranMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Tran> queryAllByCondition(Tran tran) {
        return tranMapper.selectAllByCondition(tran);
    }

    @Override
    public Tran queryByPrimaryKeyForDetails(String id) {
        return tranMapper.selectByPrimaryKeyForDetails(id);
    }
}
